<?php

namespace app\model;

use think\Model;

class Drug extends Model
{
    protected $name = 'yaopin';

    protected $pk = 'ypid';

    protected $schema = [
        'ypid'         => 'int',
        'ypname'       => 'string',
        'ypinfo'       => 'string',
        'yplv'         => 'int',
        'type'         => 'int',    //类型，1恢复，2修为，3增益

        'yphp'         => 'int',
        'ypmana'       => 'int',

        'exp'          => 'int',

        'ypgj'         => 'int',
        'ypfy'         => 'int',
        'ypbj'         => 'int',
        'ypxx'         => 'int',
        'ypwx'         => 'int',

        'ypjg'         => 'int',    //价格
    ];

    public function getTypeAttr($value)
    {
        $data = $this->type();
        return $data[$value];
    }

    public function type()
    {
        return [1=>'恢复类',2=>'修为类',3=>'增益类'];
    }

    /**
     * 获取列表
     * @return array 分页列表
     */
    public function loadList($where = [], $pageAry = [], $order = 'ypid desc')
    {
        $data = $this->where($where)->order($order)->paginate($pageAry);
        return $data;
    }

    /**
     * 获取列表
     * @return array 列表
     */
    public function getList($where = array(), $order = 'ypid desc')
    {
        $data = $this->where($where)->order($order)->select();
        return $data;
    }

    /**
     * 获取信息
     * @param int $ID
     * @return array 信息
     */
    public function getInfo($id)
    {
        $map = array();
        $map['ypid'] = $id;
        return $this->getWhereInfo($map);
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {
        $info = $this->where($where)->find();
        return $info;
    }

    /**
     * 获取字段值
     * @param array $where 条件
     * @param array $field 字段
     * @return field 信息
     */
    public function getWhereValue($where, $field)
    {
        $data = $this->where($where)->value($field);
        return $data;
    }

    /**
     * 获取列的值
     * @param array $where 条件
     * @param array $field 字段
     * @return array 信息
     */
    public function getWhereColumn($where, $field)
    {
        $data = $this->where($where)->column($field);
        return $data;
    }

    /**
     * 新增
     */
    public function addall($request)
    {
        return $this->save($_POST);
    }

    /**
     * 更新
     */
    public function editAll($request)
    {
        return $this->saved($_POST);
    }

    //数据新增
    public function add($data)
    {
        //启动事务
        $this->startTrans();
        try {
            $insertid = self::insertGetId($data);
            //提交事务
            $this->commit();
            return $insertid;
        } catch (\Exception $e) {
            //回滚事务
            $this->rollback();
        }
    }

    //数据更新
    public function saved($data)
    {
        $this->update($data);
        return true;
    }

    public function findValue($where,$field)
    {
        return $this->where($where)->value($field);
    }

    /**
     * 删除信息
     * @param int $id ID
     * @return bool 删除状态
     */
    public function del($id)
    {
        $map = array();
        $map['ypid'] = $id;
        return $this->where($map)->delete();
    }

}
